package com.qf.blog.service.serviceimpl;


import com.qf.blog.core.dao.IbaseDao;
import com.qf.blog.core.servise.impl.BaseServiceImpl;

import com.qf.blog.core.utils.PasswordUtils;
import com.qf.blog.core.utils.UUIDUtils;
import com.qf.blog.dao.UserDao;
import com.qf.blog.dao.daoimpl.UserDaoImpl;
import com.qf.blog.entity.UserEntity;
import com.qf.blog.service.UserService;

import java.util.Date;

/**
 * @author zyt
 * @version 1.0
 */
public class UserServiceImpl extends BaseServiceImpl<UserEntity> implements UserService {
    // 创建dao对象
    private UserDao userDao = new UserDaoImpl();
    // 业务层调dao层
    @Override
    protected IbaseDao getBaseDao() {
        return userDao;
    }

    /**
     * 注册用户的密码加密
     * @param userEntity
     * @throws Exception
     */
    @Override
    public void register(UserEntity userEntity) throws Exception {
        // 给注册用户输入的密码加密
        userEntity.setPassword(PasswordUtils.hashPw(userEntity.getPassword()));
        // 设置用户的状态
        userEntity.setStatus(1); // 1:未激活，2：已激活，3：冻结
        userEntity.setCreateTime(new Date()); // 注册时间  当前时间
        userEntity.setActivationCode(UUIDUtils.getId());  // 激活码
        userEntity.setType(0);  // 状态
        // 入库
        int save = userDao.insert(userEntity);
    }

    /**
     * 用户登录的密码对比
     * @param username
     * @param password
     * @return
     * @throws Exception
     */
    @Override
    public UserEntity login(String username, String password) throws Exception {
        // 根据用户的id查询
        UserEntity userEntity =userDao.selectUsernaem(username);
        System.out.println(userEntity);
        // if判断有没有查到用户
        if (userEntity== null){
            return  null;
        }
        // 查到用户后  比对密码
        if (!PasswordUtils.check(password,userEntity.getPassword())){
            System.out.println("密码错误！");
            return  null; // 密码不正确 跳出if结构

        }
        return userEntity;
    }

    /**
     * 激活码的方法
     * @param activationCode
     * @return
     */
    @Override
    public UserEntity byActivationCode(String activationCode) throws Exception {

        return userDao.selectByActivationCode(activationCode) ;
    }

    @Override
    public boolean save(UserEntity userEntity) throws Exception {


        return false;
    }

    @Override
    public boolean update(UserEntity userEntity) throws Exception {
        return false;
    }
}
